/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.Brews;
import com.openbrew.openbrewrf.Movement;
import com.openbrew.openbrewrf.views.BrewView;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import tools.MovementTools;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.movement")
public class MovementFacadeREST extends AbstractFacade<Movement> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public MovementFacadeREST() {
        super(Movement.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public Movement create(@WebParam(name = "movement") Movement entity) {
        entity = super.create(entity);
        return entity;
    }

    @POST
    @WebMethod(operationName = "create")
    @Path("createAndUpdate/{temp}/{grav}/{time}/{notes}/{vol}/{psi}")
    @Consumes({"application/xml", "application/json"})
    public Movement createMovementAndUpdateBrew(@WebParam(name = "movement") Movement entity,
            @PathParam("temp") float temperature, @PathParam("grav") float gravity,
            @PathParam("time") java.sql.Time time, @PathParam("notes") String notes,
            @PathParam("vol") float volume, @PathParam("psi") float psi) {
            entity = super.create(entity);
            final MovementTools mt = new MovementTools();
            mt.updateBrewBasedOnMovement(em, entity, temperature, gravity, time, notes, volume, psi);
            return entity;
    }

    @POST
    @WebMethod(operationName = "create")
    @Path("createAndUpdateVCheck/{temp}/{grav}/{time}/{notes}/{vol}/{psi}/{brewVersion}")
    @Consumes({"application/xml", "application/json"})
    public Movement createMovementAndUpdateBrewVersionCheck(@WebParam(name = "movement") Movement entity,
            @PathParam("temp") float temperature, @PathParam("grav") float gravity,
            @PathParam("time") java.sql.Time time, @PathParam("notes") String notes,
            @PathParam("vol") float volume, @PathParam("psi") float psi, 
            @PathParam("brewVersion") int brewVersion) {
        Brews brew = getBrew(em, entity.getMovementBrewID());
        if(brew.getVersion()==brewVersion){
            entity = super.create(entity);
            final MovementTools mt = new MovementTools();
            mt.updateBrewBasedOnMovement(em, entity, temperature, gravity, time, notes, volume, psi);
            getEntityManager().getEntityManagerFactory().getCache().evict(BrewView.class);
        }
        return entity;
    }

    /*************************************************************************
     * Database access files.
     *************************************************************************/
    private Brews getBrew(final EntityManager en, final int brewID) {
        TypedQuery<Brews> query = en.createQuery(
                "SELECT b FROM Brews b WHERE b.bid = ?1", Brews.class);
        query.setParameter(1, brewID);
        return query.getSingleResult();
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Movement entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Movement find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Movement> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Movement> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
